package com.odt.outdoor.framwork;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

/**
 * Created by shaomengjie on 2017/3/15.
 */

public class AppStateTracker implements Application.ActivityLifecycleCallbacks {
    public static final int APP_STATE_INIT_OR_KILLED = 100;
    public static final int APP_STATE_OFFLINE = 101;
    public static final int APP_STATE_ONLINE = 102;
    public static final int APP_STATE_LOGOUT = 103;
    public static final int APP_STATE_KICKOUT = 104;
    private int app_state = 100;
    private boolean isForeground;
    private int activityCount;
    private boolean isScreenOff;
    private long timeStamp;
    private static AppStateTracker mInstance;
    private Application application;
    private static final long MAX_INTERVAL = 60000L;
    private AppStateTracker.ScreenBroadcastReceiver mScreenReceiver;

    private AppStateTracker(Application application) {
        this.application = application;
        this.mScreenReceiver = new AppStateTracker.ScreenBroadcastReceiver();
        this.timeStamp = System.currentTimeMillis();
        application.registerActivityLifecycleCallbacks(this);
    }

    public static void init(Application application) {
        mInstance = new AppStateTracker(application);
    }

    public static AppStateTracker getInstance() {
        return mInstance;
    }

    public boolean isForground() {
        return this.isForeground;
    }

    public boolean isShowGesture() {
        return this.app_state == 101 && this.isScreenOff?true:this.app_state == 101 && System.currentTimeMillis() - this.timeStamp > 60000L;
    }

    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Trace.d(activity.getClass().getSimpleName() + " onActivityCreated " + (savedInstanceState != null));
    }

    public void onActivityStarted(Activity activity) {
        Trace.d(activity.getClass().getSimpleName() + " onActivityStarted");
        ++this.activityCount;
        this.isScreenOff = false;
    }

    public void onActivityResumed(Activity activity) {
        Trace.d(activity.getClass().getSimpleName() + " onActivityResumed");
        this.isForeground = true;
    }

    public void onActivityPaused(Activity activity) {
        Trace.d(activity.getClass().getSimpleName() + " onActivityPaused");
    }

    public void onActivityStopped(Activity activity) {
        Trace.d(activity.getClass().getSimpleName() + " onActivityStopped");
        --this.activityCount;
        if(this.activityCount == 0) {
            this.isForeground = false;
            this.timeStamp = System.currentTimeMillis();
        }

    }

    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        Trace.d(activity.getClass().getSimpleName() + " onActivitySaveInstanceState");
    }

    public void onActivityDestroyed(Activity activity) {
        Trace.d(activity.getClass().getSimpleName() + " onActivityDestroyed");
    }

    public void setScreenOff(boolean isOff) {
        this.isScreenOff = isOff;
    }

    public int getAppState() {
        return this.app_state;
    }

    public void setAppState(int appState) {
        this.app_state = appState;
        if(appState == 101) {
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.intent.action.SCREEN_OFF");
            filter.addAction("android.intent.action.USER_PRESENT");
            this.application.registerReceiver(this.mScreenReceiver, filter);
        } else {
            this.application.unregisterReceiver(this.mScreenReceiver);
        }

    }

    private class ScreenBroadcastReceiver extends BroadcastReceiver {
        private String action;

        private ScreenBroadcastReceiver() {
            this.action = null;
        }

        public void onReceive(Context context, Intent intent) {
            this.action = intent.getAction();
            if("android.intent.action.SCREEN_ON".equals(this.action)) {
                AppStateTracker.mInstance.setScreenOff(true);
            } else if("android.intent.action.SCREEN_OFF".equals(this.action)) {
                AppStateTracker.mInstance.setScreenOff(true);
            } else if("android.intent.action.USER_PRESENT".equals(this.action)) {
                AppStateTracker.mInstance.setScreenOff(true);
            }

        }
    }
}
